package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //查询所有类
    @GetMapping("category/findAll")
    public ResultInfo findAll() {
        //调用service查询
        List<Category> categoryList = categoryService.findAll();
        return ResultInfo.success(categoryList);
    }


    //分类新增
    @PostMapping("/category")
    public ResultInfo save(@RequestBody Category category) {
        //调用service
        categoryService.save(category);
        return ResultInfo.success();
    }

    //分类修改
    @PutMapping("/category")
    public ResultInfo update(@RequestBody Category category) {// 1.接收put参数
        // 2.调用service修改
        categoryService.update(category);
        // 3.返回结果
        return ResultInfo.success();
    }

    //删除分类
    @DeleteMapping("/category")
    public ResultInfo delete(Long id) {
        //diaoyong service
        categoryService.selete(id);
        return ResultInfo.success();
    }


    //新增菜品、套餐时查询的分类信息
    @GetMapping("/category/list")
    public ResultInfo findById(Integer type) {//接收参数
        //2 根据type查询分类
        List<Category> categoryList1 = categoryService.findById(type);
        //返回结果
        return ResultInfo.success(categoryList1);

    }

}
